/**
 *  发布订阅模式原理实现
 */

function EventEmitter() {
  this._events = {}
}

EventEmitter.prototype.on = function (eventName, callback) {
  const callbacks = this._events[eventName] || []
  callbacks.push(callback)
  this._events[eventName] = callbacks
  console.log(this._events);
}

EventEmitter.prototype.emit = function (eventName, ...args) {
  let callbacks = this._events[eventName]
  if (callbacks) {
    callbacks.forEach((cb) => cb(...args))
  }
}
module.exports = EventEmitter;


